<?php
namespace app\account\controller;

use tp5_lib\api\controller\ApiBaseController;
use tp5_lib\user\model\UserModel;
use tp5_lib\account\logic\AccountModule;
use tp5_lib\account\logic\AccountLogic;
use tp5_lib\account\model\RechargeModel;
use tp5_lib\account\model\WithdrawModel;

class ApiController extends ApiBaseController
{
    public function bindAlipay()
    {
        $user=$this->checkLogin();
        if(!$alipay=input("alipay")){
            $this->error("支付宝账号不能为空");
        }
        $user_model=new UserModel();
        if(!$user_model->db()->where(["id"=>$user["id"]])->setField("alipay",$alipay)){
            $this->error("修改数据失败");
        }
        $this->success();
    }
    public function bindWxpay()
    {
        $user=$this->checkLogin();
        if(!$wxpay=input("wxpay")){
            $this->error("微信账号不能为空");
        }
        $user_model=new UserModel();
        if(!$user_model->db()->where(["id"=>$user["id"]])->setField("wxpay",$wxpay)){
            $this->error("修改数据失败");
        }
        $this->success();
    }
    public function recharge()
    {
        $user=$this->checkLogin();
        if(!$amount=input("amount")){
            $this->error("充值金额不能为空");
        }
        if(!$way=input("way")){
            $this->error("支付方式不能为空");
        }
        $data=[
            "user_id"=>$user["id"],
            "amount"=>$amount,
            "time"=>time(),
        ];
        $model=new RechargeModel();
        if(!$id=$model->db()->insertGetId($data)){
            $this->error("创建失败");
        }

        $module=new AccountModule();
        if(!$config=$module->read("account")){
            $this->error("配置文件丢失");
        }
        if(empty($config["payment"][$way])){
            $this->error("未定义的支付方式");
        }
        $payment=$config["payment"][$way];
        $class=$payment["class"];
        /** @var Payment $object */
        $object=new $class;
        return $object->mobile("recharge-$id",$amount,"充值");
    }
    public function getRechargeList()
    {
        $user=$this->checkLogin();
        $model=new RechargeModel();
        $list=$model->db()->where(["user_id"=>$user["id"]])->select();
        $this->result($list,1);
    }
    public function withdraw(){
        $user=$this->checkLogin();
        if(!$amount=input("amount")){
            $this->error("提现金额不能为空");
        }
        if(!$way=input("way")){
            $way="";
        }

        // $module=new AccountModule();
        // if(!$config=$module->read("account")){
        //     $this->error("配置文件丢失");
        // }
        // if(empty($config["payment"][$way])){
        //     $this->error("未定义的支付方式");
        // }

        $logic=new AccountLogic();
        if(!$logic->withdraw($user["id"],$amount,$way)){
            $this->error($logic->getError());
        }
        $this->success("提交成功，请等待审核");
    }
    public function getWithdrawList()
    {
        $user=$this->checkLogin();
        $model=new WithdrawModel();
        $list=$model->db()->where(["user_id"=>$user["id"]])->select();
        $this->result($list,1);
    }
}
